package com.example.wordcount;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * Created with IntelliJ IDEA.
 * ClassName: WordCountUnbounded
 * Package: com.example.wordcount
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-07-17
 * Time: 11:10
 */

//流处理 无界流数据
public class WordCountUnbounded {
    public static void main(String[] args) throws Exception {
        //1.创建执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //2.读取数据 从socket读
        DataStreamSource<String> hadoop102 = env.socketTextStream("hadoop102", 7777);

        //3.处理数据
        SingleOutputStreamOperator<Tuple2<String, Integer>> sum =
                hadoop102.flatMap((String value, Collector<Tuple2<String, Integer>> out) -> {
                    //Lambda 存在泛型擦除的问题
            String[] s = value.split(" ");
            for (String word : s) {
                out.collect(Tuple2.of(word, 1));
            }
        })
                        .returns(Types.TUPLE(Types.STRING,Types.INT))
                .keyBy((Tuple2<String, Integer> value) -> value.f0)
                .sum(1);


        //4.输出
        sum.print();
        //5.执行
        env.execute();


    }
}
